<?php

namespace App\Http\Resources\nursery;

use App\Models\nursery\MajorOrder;
use Illuminate\Http\Resources\Json\ResourceCollection;
use SimpleSoftwareIO\QrCode\Facades\QrCode;

class OrderPageCollection extends ResourceCollection
{
    /**
     * Transform the resource into an array.
     *
     * @param \Illuminate\Http\Request $request
     * @return array
     */
    public function toArray($request)
    {

        return [
            'total' => $this->resource->total(),
            'perPage' => $this->resource->perPage(),
            'current_page' => $this->resource->currentPage(),
            'items' => $this->collection->map(function (MajorOrder $item) {
                $uri = sprintf("custom=/v1/nursery/signFor=%d",$item->id);
                $path = public_path("/code/".config('app.version').'-nursery-'.$item->id.".png");
                if (!is_file($path)){
                    QrCode::format('png')->size(600,600)->generate($uri,$path);
                }

                $url = url("/code/".config('app.version').'-nursery-'.$item->id.".png");
                return [
                    'id' => $item->id,
                    'nu' => $item->nu,
                    'real_name' => $item->check->real_name,
                    'header_img' => $item->major_user->header_img,
                    'major_phone' => $item->major_user->phone,
                    'major' => $item->major->name,
                    'img' => $item->major->img,
                    'name' => $item->name,
                    'phone' => $item->phone,
                    'star' => $item->star,
                    'price' => $item->price,
                    'status' => $item->status,
                    'status_text' => $item->status_text,
                    'code_url' => $url,
                ];

            })
        ];

    }
}
